package com.huaxinkai.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huaxinkai.entity.UserEntity;
import com.huaxinkai.test.impl.IUserDao;
import com.huaxinkai.utils.JDBCUtils;

/**
 * UserDao 实现类
 * @author liuzh
 * @date 2017年5月7日
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class UserDao implements IUserDao {

	
	@Override
	public List<UserEntity> query() {
		List list = new ArrayList();
		UserEntity u = null;
		Connection connection = null;
		PreparedStatement pre = null;
		ResultSet set = null;
		String sql = "select * from user";
		
		try{
			connection = JDBCUtils.getConnection();
			pre = connection.prepareStatement(sql);
			set = pre.executeQuery();
			
			while( set.next() ){
				u = new UserEntity();
				u.setId(set.getInt("id"));
				u.setName(set.getString("name"));
				u.setPassword(set.getString("password"));
			
				list.add(u);
			}
			
		}catch(Exception e){
			
		}
		finally {
			try {
				if(set != null){
					set.close();
				}
				if(pre != null){
					pre.close();
				}
				if(connection != null){
					connection.close();
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		return list;
	}

}
